package Aula13;

public class Pilha {
	
	private No top;
	private int size;
	
	public Pilha(){
		this.top = null;
		size = 0;
	}
	
	public void push(Object o){
		if(size == 0){
			top = new No(o);
		}else{
			No no = new No(o);
			no.setProximo(top);
			top = no;
		}
		size++;
	}
	
	public Object pop(){
		if(size == 0){
			throw new PilhaVaziaException("Pilha vazia.");
		}
		Object o = top.getChave();
		top = top.getProximo();
		size--;
		return o;
	}
	
	public boolean isEmpty(){
		return size == 0;
	}
	
	public int size(){
		return size;
	}
	
	public Object top(){
		if(isEmpty()){
			throw new PilhaVaziaException("Pilha vazia.");
		}
		return top.getChave();
	}
	
	public boolean contais(Object o){
		if(!isEmpty()){
			No no = top;
			while(no != null){
				if(no.getChave().equals(o)){
					return true;
				}
				no = no.getProximo();
			}
		}		
		return false;
	}

}
